package com.llamarun;

import com.llamarun.PauseDialog.OnMyDialogResult;
import com.llamarun.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.util.Log;
import android.view.*;
import android.widget.Toast;

public class GameActivity extends Activity{
	
	private static final String TAG = GameActivity.class.getSimpleName();
	GameView gameView;

	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        // turn off the title, set to full screen, set to landscape
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

	        //set xml to game
	        setContentView(R.layout.game);
	        
	        //get whole layout by id
	        RelativeLayout layout = (RelativeLayout)findViewById(R.id.linlayout);
	        
	        //add gameview
	        gameView = new GameView(this);
	        layout.addView(gameView);
	        
	        
	        Log.d(TAG, "onCreate");	        
	    }
	 
	 
	 @Override
	    protected void onDestroy()
	    {
	    	Log.d(TAG, "Destroying...");
	    	super.onDestroy();
	    }
	     
	    @Override
	    protected void onStop() 
	    {
	    	Log.d(TAG, "Stopping...");
	    	super.onStop();
	    }
	    @Override
	    protected void onRestart()
	    {
	    	Log.d(TAG, "restarting...");
	    	super.onRestart();
	    }
	    @Override
	    protected void onResume()
	    {
	    	Log.d(TAG, "resuming...");
	    	super.onResume();
	    }
	    @Override
	    protected void onPause()
	    {
	    	Log.d(TAG, "pausing...");
	    	super.onPause();
	    }
	 
	 /*
	  * click handler for the pause button
	  * (the one on screen with the game)
	  */
	 public void pauseClickHandler(View v)
	 {
		 Log.d(TAG, "pauseClickHandler");
		 
		 //pause the game
		 gameView.pauseGame();
		 
		 //start dialog
		 final PauseDialog pauseDialog = new PauseDialog(GameActivity.this);
         pauseDialog.show();

         // from: http://stackoverflow.com/questions/4279787/how-can-i-pass-values-androi-a-dialog-and-an-activity
         pauseDialog.setDialogResult(new OnMyDialogResult()
         {
        	 public void finish(String result)
        	 {
        		 // now you can use the 'result' on your activity
        		 Log.d(TAG, "after pause dialog: "+result);
        		 
        		 if(result.equals("restart"))
        		 {
        			 reloadActivity();
        		 }
        		 else if(result.equals("resume"))
        		 {
        			 pauseDialog.cancel();
        			 resumeActivity();
        		 }
        		 else if(result.equals("main"))
        		 {
        			 finishActivity();
        		 }
        	 }
         });
	 }
	 
	 // resume the game loop and start up the music
	 public void resumeActivity()
	 {
		 gameView.resumeGame();
	 }
	 
	public void finishActivity()
	{
		Log.d(TAG, "finishing the game activity");
		finish();
	}
	
	public void reloadActivity()
	{
		Log.d(TAG, "reloading the game activity");
		
	    Intent intent = getIntent();
	    overridePendingTransition(0, 0);
	    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
	    finish();

	    overridePendingTransition(0, 0);
	    startActivity(intent);
	}
	 
	    
}
